//
//  HttpManager.m
//  wsdlWebService
//
//  Created by wulanzhou on 2017/10/11.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

#import "HttpManager.h"
#import <AFNetworking/AFNetworking.h>
#import "XmlParseHelper.h"


@interface HttpManager()
@property (nonatomic, strong) AFHTTPSessionManager  *manager;
@property (nonatomic, strong) NSMutableArray        *arrayRequest;
@property (nonatomic, strong) NSMutableArray        *arrayHttpArgs;
@end


@implementation HttpManager

+ (HttpManager *)shareInstance {
    static dispatch_once_t  onceToken;
    static HttpManager * sSharedInstance;
    
    dispatch_once(&onceToken, ^{
        sSharedInstance = [[HttpManager alloc] init];
    });
    return sSharedInstance;
}

- (instancetype)init{

    if (self = [super init]) {
        
        //1.创建管理者对象
        _manager = [AFHTTPSessionManager manager];
    }
    return self;
}

- (void)requestArgs:(SoapArgs *)args completion:(SoapRequestFinished)finished{
    
    /***
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[args request] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"xml= %@", xml);
        }
        
    }];
    //开始启动任务
    [dataTask resume];
     ***/
    

    
    self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/xml",@"text/json",@"text/javascript",@"text/html",nil,nil];
    
    //数据任务
    NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:[args request] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        
        NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
        WBResponse *wb= [[WBResponse alloc] init];
        wb.code = [NSString stringWithFormat:@"%ld",(long)res.statusCode];
        if (error) {
            wb.success = NO;
            wb.errMsg = [error description];
            
            NSLog(@"Error: %@", error);
        } else {
            wb.success = YES;
            wb.xml = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            
            if (args.soapBody.xmlns && [args.soapBody.xmlns length]>0) {
                NSString *repStr = [wb.xml stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"xmlns=\"%@\"",args.soapBody.xmlns] withString:@""];
                wb.parse = [[XmlParseHelper alloc] initWithData:repStr];
            }else{
               wb.parse = [[XmlParseHelper alloc] initWithData:wb.xml];
            }
            
            wb.content = [wb.parse soapMessageReturnXml:args.soapBody.methodName];
        }
        
        if (finished) {
            finished(wb);
        }
       
    }];
    //开始启动任务
    [dataTask resume];
     
     

}

@end
